gusucode.com > VC 点对点文件传输实例 > VC 点对点文件传输实例/code/www.NewXing.com/Client_FileTransfer/Client_FileTransferDlg.cpp

    // Client_FileTransferDlg.cpp : implementation file
#include "stdafx.h"
#include "Client_FileTransfer.h"
#include "Client_FileTransferDlg.h"
// Download by http://www.NewXing.com
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CClient_FileTransferDlg dialog

CClient_FileTransferDlg::CClient_FileTransferDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CClient_FileTransferDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CClient_FileTransferDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CClient_FileTransferDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClient_FileTransferDlg)
	DDX_Control(pDX, IDC_LIST_FILELIST, m_ListFile);
	DDX_Control(pDX, IDC_IPADDRESS1, m_IPCtrl);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CClient_FileTransferDlg, CDialog)
	//{{AFX_MSG_MAP(CClient_FileTransferDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_FILELIST, OnDblclkListFilelist)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_RECVFILELIST, OnFileList)
	ON_MESSAGE(WM_FILESTATUS, OnFileStatus)
	ON_MESSAGE(WM_CONNECTERROR, OnConnectError)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClient_FileTransferDlg message handlers

BOOL CClient_FileTransferDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	m_IPCtrl.SetAddress(127, 0, 0, 1);//设置服务器IP
	InitListColumn();
	m_pClient = new CTCPClient_FT(this);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CClient_FileTransferDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CClient_FileTransferDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//初始化文件列表
void CClient_FileTransferDlg::InitListColumn()
{
	int iWidth = 50;
	m_ListFile.InsertColumn(0, "File Name", LVCFMT_LEFT, 3*iWidth, -1);
	m_ListFile.InsertColumn(1, "File Size", LVCFMT_LEFT, 2*iWidth, -1);
	m_ListFile.InsertColumn(2, "Status",    LVCFMT_LEFT, 2*iWidth, -1);
	m_ListFile.InsertColumn(3, "Velocity",  LVCFMT_LEFT, 2*iWidth, -1);//速度列
	m_ListFile.InsertColumn(4, "Time",      LVCFMT_LEFT, 2*iWidth, -1);
	m_ListFile.InsertColumn(5, "Server Path", LVCFMT_LEFT, 3*iWidth, -1);//服务器路径
	m_ListFile.InsertColumn(6, "Start Tick", LVCFMT_LEFT, 2*iWidth, -1);//开始记号

	m_ListFile.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
}

//连接按钮
void CClient_FileTransferDlg::OnButtonConnect() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	
	BYTE i1,i2,i3,i4;
	m_IPCtrl.GetAddress(i1,i2,i3,i4);

	char sIP[15];
	sprintf(sIP, "%d.%d.%d.%d", i1,i2,i3,i4);//显示IP地址

	m_ListFile.DeleteAllItems();//文件列表,清除所有元素

	m_pClient->SetServerIP(sIP);
	m_pClient->SetServerPort(7000);

	MSGREQUEST msgRequest;
	msgRequest.iCommand = FILELIST;//请求文件列表
	msgRequest.lFileOffset = 0;
	msgRequest.lFileLength = 0;
	strcpy(msgRequest.sClientPath, "\0");
	strcpy(msgRequest.sServerPath, "\0");

	m_pClient->RequestFile(msgRequest);//调用请求线程
}

//双击文件列表
void CClient_FileTransferDlg::OnDblclkListFilelist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	NMLISTVIEW* pListView = (NMLISTVIEW*)pNMHDR;//结构体,ListView的通知信息

	int iSel = pListView->iItem;
	
	if(iSel == -1)
		return;
	
	CString strFileName = m_ListFile.GetItemText(iSel, 0);
	CFileDialog dlg(FALSE, NULL, strFileName.GetBuffer(0), OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "All Files(*.*)|*.*||", this);
	//打开一个保存对话框,隐藏readonly||覆盖确认
	if(dlg.DoModal() != IDOK)
		return;

	CString strClientPath = dlg.GetPathName();
	//得到客户端保存路径
	CFile file;
	BOOL bOpen = file.Open(strClientPath, CFile::modeCreate|CFile::modeWrite, NULL);
	
	if(!bOpen)
	{
		MessageBox("文件创建或打开失败!请确认路径再重试!");
		return;
	}
	file.Close();

	MSGREQUEST msgRequest;//通信语言结构体
	msgRequest.iCommand = FILEDATA;
	msgRequest.lFileLength = m_ListFile.GetItemData(iSel);
	msgRequest.lFileOffset = 0;
	strcpy(msgRequest.sClientPath, strClientPath.GetBuffer(0));
	strcpy(msgRequest.sServerPath, m_ListFile.GetItemText(iSel, 5)); 
	//子项目,第6列
	
	DWORD dwStartTick = GetTickCount();
	char sStartTick[20];
	sprintf(sStartTick, "%u", dwStartTick);
	//格式:Unsigned decimal integer
	m_ListFile.SetItemText(iSel, 6, sStartTick);
	
	m_pClient->RequestFile(msgRequest);//调用请求线程

	*pResult = 0;
}

void CClient_FileTransferDlg::OnButtonExit() 
{
	// TODO: Add your control notification handler code here
	OnOK();
}

LRESULT CClient_FileTransferDlg::OnFileStatus(WPARAM wParam, LPARAM lParam)
{
	MSGFILESTATUS* pmsg = (MSGFILESTATUS*)wParam;
	CString strPath;
	strPath.Format("%s", pmsg->sServerPath);
	int iTotal = m_ListFile.GetItemCount();
	int iSel = 0;
	for(; iSel<iTotal; iSel++)
	{
		CString strListPath;
		strListPath = m_ListFile.GetItemText(iSel, 5);
		if(strPath.Compare(strListPath) == 0)//路径相同
			break;
	}
	if(iSel == iTotal)
		return 1;

	long lFileLength = m_ListFile.GetItemData(iSel);

	CString strStatus;
	strStatus.Format("%0.2f%%", (float)pmsg->lFileOffset/lFileLength*100);
	//显示状态 %,精确到小数点后两位
	m_ListFile.SetItemText(iSel, 2, strStatus.GetBuffer(0));

	//显示时间
	DWORD dwNowTick, dwStartTick;//毫秒
	dwNowTick = GetTickCount();
	sscanf(m_ListFile.GetItemText(iSel, 6), "%u", &dwStartTick);

	long iEllapsed = (dwNowTick-dwStartTick);//间隔:秒
	strStatus.Format("%dms", iEllapsed);
	m_ListFile.SetItemText(iSel, 4, strStatus.GetBuffer(0));
	
	//显示速度
	if(iEllapsed > 0)
	{
		strStatus.Format("%dk/s", (pmsg->lFileOffset/1024/iEllapsed)*1000);
		m_ListFile.SetItemText(iSel, 3, strStatus.GetBuffer(0));
	}


	return 0;
}

LRESULT CClient_FileTransferDlg::OnConnectError(WPARAM wParam, LPARAM lParam)
{
	MessageBox("连接服务器失败!");
	return 0;
}

//
LRESULT CClient_FileTransferDlg::OnFileList(WPARAM wParam, LPARAM lParam)
{
	MSGFILELIST* pmsgList = (MSGFILELIST*)wParam;
	int iItem = m_ListFile.GetItemCount();
	CString strServerPath, strFileName, strFileSize;
	strServerPath.Format("%s", pmsgList->sServerPath);
	int iPos = strServerPath.ReverseFind('\\');
	//Searches this CString object for the last match of a substring.
	//设置文件名到缓冲区
	strFileName = strServerPath.Right(strServerPath.GetLength()-iPos-1);

	strFileSize.Format("%0.2fk", (float)pmsgList->lFileLength/1024);
	
	LV_ITEM lvi;
	lvi.mask = LVIF_TEXT|LVIF_PARAM;
	lvi.iItem = iItem;
	lvi.iSubItem = 0;
	lvi.lParam = pmsgList->lFileLength;
	//显示
	lvi.pszText = strFileName.GetBuffer(0);
	m_ListFile.InsertItem(&lvi);
	m_ListFile.SetItemText(iItem, 1, strFileSize.GetBuffer(0));
	m_ListFile.SetItemText(iItem, 2, "0.00%");
	m_ListFile.SetItemText(iItem, 3, "0k/s");
	m_ListFile.SetItemText(iItem, 4, "0s");
	m_ListFile.SetItemText(iItem, 5, strServerPath.GetBuffer(0));
	m_ListFile.SetItemText(iItem, 6, "0");
	return 0;
}

void CClient_FileTransferDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	if(m_pClient != NULL)
	{
		m_pClient->StopAllRecvThread();
		delete m_pClient;
		m_pClient = NULL;//指针置空,安全
	}
}